package com.itheima.reggie.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.SetmealDishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper,Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private DishService dishService;

    /**
     * 新增套餐
     * @param setmealDto
     */
    @Override
    @Transactional
    public void saveWithDish(SetmealDto setmealDto) {
        //保存套餐数据到 setmeal表中 ： SetmealMapper
        this.save(setmealDto);
        List<SetmealDish> setmealDishList = setmealDto.getSetmealDishes();
        setmealDishList.stream().map((item) ->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        //保存套餐和菜品信息
        setmealDishService.saveBatch(setmealDishList);

    }

    /**
     * 删除套餐数据
     * @param ids
     */
    @Override
    @Transactional
    public void removeWithDish(List<Long> ids) {
        //判断当前要删除的套餐是否处于售卖状态，如果是，不允许删除
        //select count(*) from setmeal where id in (1,2,3) and status = 1
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids).eq(Setmeal::getStatus,1);
        //获取套餐里菜品数量
        int count = this.count(queryWrapper);
        //判断套餐里是否有菜品
        if(count>0){
            //在售卖，不能删除，抛出异常
            throw new CustomException("套餐正在售卖中，不能删除！");
        }
        //可以删除
        this.removeByIds(ids);
        //删除套餐： delete from setmeal where id in (1,2,3)
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(SetmealDish::getSetmealId,ids);
        //删除套餐对应的菜品数据：delete from setmeal_dish where setmeal_id in (1,2,3)
        setmealDishService.remove(lambdaQueryWrapper);
    }

    /**
     * 根据套餐id查询套餐数据
     * @param id
     * @return
     */
    @Override
    public SetmealDto getByIdWithDish(Long id) {
        //根据套餐id查询套餐数据 select * from setmeal where id =?
        Setmeal setmeal = this.getById(id);
        //根据套餐id查询套餐对应的菜品数据 select * from setmeal_dish where setmeal_id = ?
        //将套餐 setmeal 和套餐菜品集合 setmealDishList 数据封装到 SetmealDto并返回
        //首先new一个SetmealDto对象
        SetmealDto setmealDto = new SetmealDto();
        //BeanUtils.copyProperties() 拷贝setmeal到setmealDto中
        BeanUtils.copyProperties(setmeal,setmealDto);
        //将setmealDishList 设置到 setmealDto中
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmeal.getId());
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);
        return setmealDto;
    }

    /**
     * 修改套餐信息
     * @param setmealDto
     */
    @Override
    @Transactional
    public void updateWithDish(SetmealDto setmealDto) {
        //根据id更新 setmeal 套餐表数据 update setmeal set xx=?,.. where id =?
        this.updateById(setmealDto);
        //操作套餐对应的菜品数据： setmeal_dish
        //先根据套餐id删除套餐对应的菜品数据： delete from setmeal_dish where setmeal_id =?
        //获取套餐id   setmealDto.getId()
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //使用 setmealDishMapper 根据套餐id进行删除 ：delete from setmeal_dish where setmeal_id =?
        //重新添加新的套餐对应的菜品数据 insert setmeal_dish values (..)
        //setmealDto.getSetmealDishes() 获取套餐的菜品集合 List<SetmealDish>
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        setmealDishes = setmealDishes.stream().map((item) -> {
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 后台根据套餐id查询套餐菜品数据
     * @param id
     * @return
     */
    @Override
    public List getDishId(Long id) {
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        List<SetmealDishDto> setmealDishDtos = new ArrayList<>();
        for (SetmealDish setmealDish : list) {
            SetmealDishDto setmealDishDto = new SetmealDishDto();
            BeanUtil.copyProperties(setmealDish, setmealDishDto);
            Long dishId = setmealDish.getDishId();
            Dish dish = dishService.getById(dishId);
            String image = dish.getImage();
            setmealDishDto.setImage(image);
            setmealDishDtos.add(setmealDishDto);
        }
        return setmealDishDtos;
    }
}